<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="example">
        <!-- 绑定 HTML Class: -->
        <!-- 对象语法：可以传给 v-bind:class 一个对象，以动态地切换 class -->
        <div v-bind:class="classObject">class</div>
        <!-- 数组语法 -->
        <div v-bind:class="[activeClass, errorClass]"></div>
        <div v-bind:class="[isActive ? activeClass : '', errorClass]"></div>
        <!-- 当有多个条件 class 时这样写有些繁琐。所以在数组语法中也可以使用对象语法： -->
        <div v-bind:class="[{ active: isActive }, errorClass]"></div>


        <!-- 绑定内联样式: -->
        <!-- 对象语法 -->
        <div v-bind:style="{ color: activeColor, fontSize: fontSize + 'px' }"></div>
        <div v-bind:style="styleObject"></div>

    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script>
        var vm = new Vue({
            el: '#example',
            data() {
                return {
                    isActive: true,
                    error: null,
                    activeClass: 'active',
                    errorClass: 'text-danger',
                    activeColor: 'red',
                    fontSize: 30
                }
            },
            computed: {
                classObject: function () {
                    return {
                        active: this.isActive && !this.error,
                        //error是自己写的对象 ,type是自定义的
                        'text-danger': this.error && this.error.type === 'fatal'
                    }
                }
            }
        })
    </script>
</body>

</html>